﻿using System;
using Assist;
using Autodesk.Revit.DB;

namespace YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim
{
	public class CurveDrawing
	{
        public static ModelCurve CreateModelCurve(Curve curCurve, Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.Color color, int nWideIndex = 1)
		{
			ModelCurve result;
			try
			{
				XYZ endPoint = curCurve.GetEndPoint(0);
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, endPoint);
				SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
				if (curCurve.Length < doc.Application.ShortCurveTolerance)
				{
					result = null;
				}
				else
				{
					ModelCurve modelCurve = doc.Create.NewModelCurve(curCurve, sketchPlane);
					GraphicsStyle graphicsStyle = CurveDrawing.CreateLineStyle(color, doc, nWideIndex);
					if (graphicsStyle != null)
					{
						modelCurve.LineStyle = graphicsStyle;
					}
					result = modelCurve;
				}
			}
			catch
			{
				throw;
			}
			return result;
		}

        public static GraphicsStyle CreateLineStyle(Autodesk.Revit.DB.Color color, Autodesk.Revit.DB.Document doc, int nWideIndex = 1)
		{
			string text = "tempLineStyle ";
			text = string.Concat(new string[]
			{
				text,
				color.Red.ToString(),
				"-",
				color.Green.ToString(),
				"-",
				color.Blue.ToString()
			});
			return CurveDrawing.CreateLineType(doc, text, color, nWideIndex);
		}

        public static GraphicsStyle CreateLineType(Autodesk.Revit.DB.Document doc, string name, Autodesk.Revit.DB.Color color, int nWideIndex, GraphicsStyleType type = (GraphicsStyleType)1)
		{
			GraphicsStyle result = null;
			try
			{
				Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
				Category category2;
				if (category.SubCategories.Contains(name))
				{
					category2 = category.SubCategories.get_Item(name);
				}
				else
				{
					category2 = doc.Settings.Categories.NewSubcategory(category, name);
				}
				category2.LineColor = color;
				category2.SetLineWeight(nWideIndex, type);
				result = category2.GetGraphicsStyle(type);
			}
			catch
			{
				result = null;
			}
			return result;
		}
	}
}
